package com.yami.shop.mp.common.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.config.WxConfig;
import com.yami.shop.mp.common.dao.WeixinMaterialMapper;
import com.yami.shop.mp.common.model.WeixinMaterial;
import com.yami.shop.mp.common.platform.service.WeixinMaterialService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author lgh on 2019/05/14.
 */
@Service
@AllArgsConstructor
public class WeixinMaterialServiceImpl extends ServiceImpl<WeixinMaterialMapper,WeixinMaterial> implements WeixinMaterialService {

    private final WeixinMaterialMapper weixinMaterialMapper;

    private final WxConfig wxConfig;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insertBatch(List<WeixinMaterial> allMaterial, String type) {
        List<WeixinMaterial> materialList = list(new LambdaQueryWrapper<WeixinMaterial>().eq(WeixinMaterial::getMediaType,type));
        if(CollectionUtils.isEmpty(materialList)){
            saveBatch(allMaterial);
            return;
        }
        List<String> dbIds = materialList.stream().map(WeixinMaterial::getMediaId).collect(Collectors.toList());
        List<WeixinMaterial> insertInfo = new ArrayList<>();
        List<WeixinMaterial> updateInfo = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(allMaterial)){
            for (WeixinMaterial weixinMaterial : allMaterial) {
                if (dbIds.contains(weixinMaterial.getMediaId())) {
                    weixinMaterial.setUpdateTime(new Date());
                    weixinMaterial.setCreateTime(null);
                    updateInfo.add(weixinMaterial);
                }else{
                    insertInfo.add(weixinMaterial);
                }
            }
        }
        if(CollectionUtils.isNotEmpty(updateInfo)){
            updateBatchById(updateInfo);
        }
        if(CollectionUtils.isNotEmpty(insertInfo)){
            saveBatch(insertInfo);
        }
//        WeixinMaterial record = new WeixinMaterial();
//        record.setMediaType(type);
//        weixinMaterialMapper.deleteById(record.getMediaId());
//        if (CollectionUtil.isNotEmpty(allMaterial)) {
//            weixinMaterialMapper.insertBatch(allMaterial);
//        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void delete(String mediaId) {
        weixinMaterialMapper.deleteById(mediaId);
        try {
            wxConfig.getWxMpService().getMaterialService().materialDelete(mediaId);
        } catch (WxErrorException e) {
            throw new YamiShopBindException(e.getError().getErrorMsg());
        }
    }

}
